﻿Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient
Partial Class adduser
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo hErr
        Dim sSql As String
        sSql = "select count(*) from admin where username ='" & Replace(TextBox2.Text, "'", "''") & "'"
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        myConn.Open()
        Dim MyCommand As New SqlCommand(sSql, myConn)
        If MyCommand.ExecuteScalar() > 0 Then
            Label1.Text = "User nay da duoc dang ky xin vui long chọn nick khac"
        Else
            If Not addUser1() Then
                Label1.Text = "Gap mot loi khi them vao mot user moi"
            Else
                Label1.Text = "Them user thanh cong"
            End If
        End If
hErr:
        Exit Sub
    End Sub
    Private Function addUser1() As Boolean
        On Error GoTo hErr

        Dim sSql As String
        ' Insert into
        sSql = " insert into admin (hoten,username,password,quyen)" & _
        "values(@hoten,@username,@password,@quyen)"
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        MyCommand.Parameters.Add(New SqlParameter("@hoten", TextBox1.Text))
        MyCommand.Parameters.Add(New SqlParameter("@username", TextBox2.Text))
        MyCommand.Parameters.Add(New SqlParameter("@password", FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox3.Text, "MD5")))
        MyCommand.Parameters.Add(New SqlParameter("@quyen", CInt(TextBox5.Text)))
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Return True
        Exit Function
hErr:
        Return False
    End Function
End Class
